package com.shangyueshang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shangyueshang.common.SysConstants;
import com.shangyueshang.mapper.OrderMapper;
import com.shangyueshang.model.Order;
import com.shangyueshang.request.PageReq;
import com.shangyueshang.response.PageResult;
import com.shangyueshang.service.OrderService;
import com.shangyueshang.utils.DateUtil;
import com.shangyueshang.utils.MyPageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.YearMonth;
import java.util.List;
import java.util.Map;

/**
 * @author: JokeyZheng
 * @email: zhengjingfeng@ruqimobility.com
 * @created: 2022/1/14 18:49
 * @version: v1.0.0
 */
@Slf4j
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
	
	@Override
	public PageResult findPage(PageReq pageReq) {
		QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
		queryWrapper.orderByDesc(SysConstants.trade_date);
		Map<String, Object> queryParams = pageReq.getLineParams();
		queryWrapper.in(SysConstants.shop, (List) queryParams.remove(SysConstants.shop));
		queryParams.forEach((k, v) -> {
			if (k.equals(SysConstants.from_trade_month)) {
				YearMonth month = DateUtil.parseYYYMM(v.toString());
				queryWrapper.ge(SysConstants.trade_date, month.atDay(1));
			} else if (k.equals(SysConstants.to_trade_month)) {
				YearMonth month = DateUtil.parseYYYMM(v.toString());
				queryWrapper.le(SysConstants.trade_date, month.atEndOfMonth());
			} else {
				queryWrapper.eq(k, v);
			}
		});
		PageInfo<Order> pageInfo =
				PageHelper.startPage(
						pageReq.getPageNum(),
						pageReq.getPageSize()
				).doSelectPageInfo(() -> super.list(queryWrapper));
		return MyPageHelper.getPageResult(pageInfo);
	}
}
